<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>企业微信登录测试</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .container {
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.1);
            padding: 40px;
            width: 100%;
            max-width: 500px;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .logo {
            font-size: 48px;
            margin-bottom: 10px;
        }

        .title {
            font-size: 24px;
            font-weight: 600;
            color: #333;
            margin-bottom: 5px;
        }

        .subtitle {
            font-size: 14px;
            color: #666;
        }

        .test-section {
            margin-bottom: 30px;
        }

        .test-section h3 {
            font-size: 18px;
            color: #333;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #f0f0f0;
        }

        .btn {
            width: 100%;
            padding: 14px;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .btn-primary {
            background: #07C160;
            color: white;
        }

        .btn-primary:hover {
            background: #06AD56;
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(7, 193, 96, 0.4);
        }

        .btn-secondary {
            background: #667eea;
            color: white;
        }

        .btn-secondary:hover {
            background: #5a6fd8;
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
        }

        .btn-info {
            background: #f8f9fa;
            color: #333;
            border: 1px solid #ddd;
        }

        .btn-info:hover {
            background: #e9ecef;
        }

        .status {
            padding: 15px;
            border-radius: 6px;
            margin-bottom: 20px;
            font-size: 14px;
            display: none;
        }

        .status.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .status.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .status.info {
            background: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }

        .user-info {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 6px;
            margin-top: 15px;
            font-size: 14px;
            display: none;
        }

        .user-info pre {
            background: white;
            padding: 10px;
            border-radius: 4px;
            overflow-x: auto;
            font-size: 12px;
            margin-top: 10px;
        }

        .config-info {
            background: #fff3cd;
            color: #856404;
            padding: 15px;
            border-radius: 6px;
            margin-bottom: 20px;
            font-size: 14px;
            line-height: 1.5;
        }

        .config-info strong {
            display: block;
            margin-bottom: 5px;
        }

    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <div class="logo">🔧</div>
        <div class="title">企业微信登录测试</div>
        <div class="subtitle">测试企业微信集成功能</div>
    </div>

    <div class="config-info">
        <strong>当前配置信息：</strong>
        CorpId: ww190f01ca9ea550b1<br>
        AgentId: 1000021<br>
        回调地址: /evaluation/wechat/callback
    </div>

    <div class="status" id="status"></div>

    <div class="test-section">
        <h3>🚀 登录测试</h3>
        <button class="btn btn-primary" onclick="startWeChatLogin()">
            <span>💬</span>
            开始企业微信登录
        </button>
        <button class="btn btn-secondary" onclick="checkLoginStatus()">
            <span>👤</span>
            检查登录状态
        </button>
        <button class="btn btn-info" onclick="logout()">
            <span>🚪</span>
            退出登录
        </button>
    </div>

    <div class="test-section">
        <h3>🔍 API测试</h3>
        <button class="btn btn-info" onclick="testUserInfo()">
            获取用户信息
        </button>
        <button class="btn btn-info" onclick="testConfig()">
            测试配置
        </button>
        <button class="btn btn-info" onclick="testErrorPage()">
            测试错误页面
        </button>
    </div>

    <div class="user-info" id="userInfo">
        <strong>用户信息：</strong>
        <pre id="userInfoContent"></pre>
    </div>
</div>

<script>
        // 显示状态信息
        function showStatus(message, type = 'info') {
            const statusDiv = document.getElementById('status');
            statusDiv.textContent = message;
            statusDiv.className = `status ${type}`;
            statusDiv.style.display = 'block';

            // 3秒后自动隐藏
            setTimeout(() => {
                statusDiv.style.display = 'none';
            }, 3000);
        }

        // 显示用户信息
        function showUserInfo(data) {
            const userInfoDiv = document.getElementById('userInfo');
            const userInfoContent = document.getElementById('userInfoContent');
            userInfoContent.textContent = JSON.stringify(data, null, 2);
            userInfoDiv.style.display = 'block';
        }

        // 隐藏用户信息
        function hideUserInfo() {
            document.getElementById('userInfo').style.display = 'none';
        }

        // 开始企业微信登录
        function startWeChatLogin() {
            showStatus('正在跳转到企业微信登录...', 'info');
            window.location.href = '/evaluation/wechat/login';
        }

        // 检查登录状态
        async function checkLoginStatus() {
            try {
                showStatus('正在检查登录状态...', 'info');

                const response = await fetch('/evaluation/wechat/userinfo', {
                    method: 'GET',
                    credentials: 'include'
                });

                const result = await response.json();

                if (result.success && result.data) {
                    showStatus('用户已登录', 'success');
                    showUserInfo(result.data);
                } else {
                    showStatus('用户未登录', 'error');
                    hideUserInfo();
                }
            } catch (error) {
                console.error('检查登录状态失败:', error);
                showStatus('检查登录状态失败: ' + error.message, 'error');
                hideUserInfo();
            }
        }

        // 退出登录
        async function logout() {
            try {
                showStatus('正在退出登录...', 'info');

                const response = await fetch('/evaluation/wechat/logout', {
                    method: 'GET',
                    credentials: 'include'
                });

                const result = await response.json();

                if (result.success) {
                    showStatus('退出登录成功', 'success');
                    hideUserInfo();
                } else {
                    showStatus('退出登录失败: ' + result.message, 'error');
                }
            } catch (error) {
                console.error('退出登录失败:', error);
                showStatus('退出登录失败: ' + error.message, 'error');
            }
        }

        // 测试获取用户信息API
        async function testUserInfo() {
            try {
                showStatus('正在获取用户信息...', 'info');

                const response = await fetch('/evaluation/wechat/userinfo', {
                    method: 'GET',
                    credentials: 'include'
                });

                const result = await response.json();
                showUserInfo(result);

                if (result.success) {
                    showStatus('获取用户信息成功', 'success');
                } else {
                    showStatus('获取用户信息失败: ' + result.message, 'error');
                }
            } catch (error) {
                console.error('获取用户信息失败:', error);
                showStatus('获取用户信息失败: ' + error.message, 'error');
                showUserInfo({ error: error.message });
            }
        }

        // 测试配置
        function testConfig() {
            const config = {
                corpId: 'ww190f01ca9ea550b1',
                agentId: 1000021,
                redirectUri: window.location.origin + '/evaluation/wechat/callback',
                loginUrl: window.location.origin + '/evaluation/wechat/login',
                userInfoUrl: window.location.origin + '/evaluation/wechat/userinfo',
                logoutUrl: window.location.origin + '/evaluation/wechat/logout',
                errorPageUrl: window.location.origin + '/evaluation/wechat-error.html'
            };

            showUserInfo(config);
            showStatus('配置信息已显示', 'info');
        }

        // 测试错误页面
        function testErrorPage() {
            const testError = '这是一个测试错误信息：企业微信授权登录失败，请联系管理员。';
            const testCode = 'TEST_ERROR';

            const errorUrl = `/evaluation/wechat-error.html?error=${encodeURIComponent(testError)}&code=${testCode}`;

            showStatus('正在跳转到错误页面...', 'info');
            window.open(errorUrl, '_blank');
        }

        // 页面加载时自动检查登录状态
        document.addEventListener('DOMContentLoaded', function() {
            // 检查URL参数
            const urlParams = new URLSearchParams(window.location.search);
            const error = urlParams.get('error');
            const success = urlParams.get('success');

            if (error) {
                showStatus('登录失败: ' + decodeURIComponent(error), 'error');
            } else if (success) {
                showStatus('登录成功', 'success');
                checkLoginStatus();
            } else {
                checkLoginStatus();
            }
        });

</script>
</body>
</html>

